package com.tkjt.pms.calc.formula.utils;

public class FormulaUtils {

	public static final String formatDataKey(String origKey) {
		char lastchar = origKey.charAt(origKey.length() - 1);
		if (lastchar == ',' || lastchar == ':') {
			origKey = origKey.substring(0, origKey.length() - 1);
		}
		int index = origKey.indexOf('@');
		if (index > 0) {
			origKey = origKey.substring(0, index);
		}
		return origKey.trim();
	}

	public static final int formatCalcMonth(Object calcMonth) {
		if (calcMonth instanceof Integer) {
			return ((Integer) calcMonth).intValue();
		} else if (calcMonth instanceof String) {
			String tempValue = (String) calcMonth;
			tempValue = tempValue.toLowerCase();
			if (tempValue.charAt(0) == 'm') {
				tempValue = tempValue.substring(1);
			}
			if (tempValue.charAt(0) == '0') {
				tempValue = tempValue.substring(1);
			}
			return Integer.valueOf(tempValue);
		}
		return 0;
	}

	public static final int getQiStartMonth(int calcQi) {
		return (calcQi - 1) * 3 + 1;
	}

	public static final int findFormulaItemKeyStartPosition(String itemKey, String formula) {
		int index = 0;
		do {

			index = formula.indexOf(itemKey, index);
			if (index < 0) {
				return index;
			}
			int chechIndex = index + itemKey.length();
			if (chechIndex >= formula.length()) {
				return index;
			}
			char checkChar = formula.charAt(chechIndex);
			// 公式取数配置中的字符只能为下面几种符号, 避免出理公式取数据配置中的子串问题
			if (!(checkChar >= 'A' && checkChar <= 'Z' || checkChar >= 'a' && checkChar <= 'z' || checkChar >= '0' && checkChar <= '9' || checkChar == '@')) {
				return index;
			}

		} while (true);
	}
}
